package com.example.admin_oa.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.admin_oa.model.mapper.SysUserMapper;
import com.example.admin_oa.shiro.JwtToken;
import com.example.admin_oa.shiro.JwtUtil;
import com.example.comment_oa.dubbo.SysEmployeeServiceDubbo;
import com.example.comment_oa.pojos.SysEmployee;
import com.example.comment_oa.pojos.SysUser;
import com.example.comment_oa.response.AjaxResponse;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("adminOa/User")
@CrossOrigin
public class LoginContronller {
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @DubboReference(version = "1.0.0")
    SysEmployeeServiceDubbo sysEmployeeServiceDubbo;

    @PostMapping("/login/{username}/{userPassword}")
    public AjaxResponse login(@PathVariable("username") String username,
                              @PathVariable("userPassword") String userPassword){
        stringRedisTemplate.delete("roles");
        stringRedisTemplate.delete("permission");
        String jwt = JwtUtil.createJWT(username, "back", "user", 1000 * 60 * 60 *2);
        Subject subject = SecurityUtils.getSubject();
        JwtToken jwtToken = new JwtToken(jwt, userPassword);
        subject.login(jwtToken);
        subject.hasRole("登录");
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("user_no",username).or().eq("user_phone",username);
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        Set<String> userPermission = sysUserMapper.findUserPermissionByid(sysUser.getUserId());
        SysEmployee sysEmployee = sysEmployeeServiceDubbo.selectEmpId(sysUser.getEmpId());
        sysUser.setSysEmployee(sysEmployee);
        sysUser.setUserPassword(null);
        Map<String,Object> map=new HashMap<>();
        map.put("user",sysUser);
        map.put("token",jwt);
        map.put("permission",userPermission);
        stringRedisTemplate.opsForValue().set("user", JSON.toJSONString(sysUser));
        stringRedisTemplate.opsForValue().set("token",jwt,2, TimeUnit.HOURS);
        return AjaxResponse.success(map);
    }

    //生成验证码
    @PostMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //生成图形验证码，大小和字节数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        //设置字体
        specCaptcha.setFont(Captcha.FONT_1);
        //生成请求码
        String id = "tupian";
        //id可以是登录用户名/手机号
        response.setHeader("id", id);
        System.out.println(id);
        //输出图形码
        CaptchaUtil.out(specCaptcha, request, response);
        //获取图形码的文字
        String verCode = specCaptcha.text().toLowerCase();
        System.out.println(verCode);
        //不设置过期
        //redisTemplate.opsForValue().set(id, verCode);
        //存入redis数据库，设置一分钟过期
//        stringRedisTemplate.opsForValue().set(id,verCode,1, TimeUnit.MINUTES);
    }
}
